var MAP = 'resultMap';

function verify() {
	var checklist = [$('#model'), $('input[id^="cpuSeries"]'), $('input[id^="memoryFrequency"]'), $('input[id^="expansionSlot"]'), 
					$('input[id^="ioInterface"]')];
	for (var i in checklist) {
		var input = checklist[i];
		if (input.attr('type') == 'checkbox') {
			if (input.filter(':checked').length == 0) {
				alert('请至少选择一个' + input.parents('tr').find('th').text().replace('：','！'));
				return false;
			}
		} else {
			if (input.val() == '') {
				alert('请输入' + input.parents('tr').find('th').text().replace('：','！'));
				return false;
			}
		}
	}
	return true;
}

function add() {
	if (verify()) {
		var inputList = [$('#model'), $('#brand'), $('#chipset'), $('#cpuManufacturer'), $('input[id^="cpuSeries"]:checked'), $('#cpuSlot'), 
						$('#maxMemoryCapacity'), $('#memoryType'), $('input[id^="memoryFrequency"]:checked'), $('#memoryChannel'), $('#memorySlot'), 
						$('#memorySlotNum'), $('#soundCard'), $('#networkCard'), $('#raid'), $('input[id^="expansionSlot"]:checked'), 
						$('input[id^="ioInterface"]:checked'), $('#type'), $('#size'),  $('#powerInterface'), $('#powerMode'), $('#feature'), 
						$('#attachment'), $('#qaPhone'), $('#qaTime'), $('#qaPolicy'), $('#qaDetail'), $('#price'), $('#picture')];
		var parameters = new Object();
		for (var i in inputList) {
			var input = inputList[i];
			if (input.attr('type') == 'checkbox') {
				input.each(function(index) { 
					var checkboxSelect = $(this).siblings('select');
					if (checkboxSelect.length == 0) {
						parameters[MAP+'['+$(this).attr('name')+']['+index+']'] = $(this).val();
					} else {
						parameters[MAP+'['+$(this).attr('name')+']['+index+']'] = $(this).val() + '@' + checkboxSelect.val();
					}
				});
			} else {
				parameters[MAP+'['+input.attr('name')+'][0]'] = input.val();
			}
		}
		/*-----=====浏览器控制台输出=====-----
		for (var v in parameters) {
			console.log(v+': '+parameters[v]);
		}
		-----=========================-----*/
		$.post(BASEPATH+'/supplier/addMainBoard.do', parameters, function(data) {
			if (data == '添加成功！') {
				window.location.href = BASEPATH+'/supplier/mainBoardList.do';
			} else {
				alert(data);
			}
		}, 'text');
	}
}

$(document).ready(function() {
	$('.checkbox').click(function() {
		if ($(this).is(':checked')) {
			$('#'+$(this).attr('id')+'Num').removeAttr('disabled');
		} else {
			$('#'+$(this).attr('id')+'Num').attr('disabled', 'disabled');
		}
	});
});